<?php 

Class articleModel extends appModel
{
	
	function get_art_list($page=1,$id,$page_size=10,$cfg=array())
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$sql="SELECT parent_id FROM ".$this->table('category')." WHERE id = '".$id."'";
		$parent_id=$this->getOne($sql);
		if($id)
		{
			
				$sql="SELECT id FROM ".$this->table('category')." WHERE parent_id = '".$id."'";
				$temp=$this->getAll($sql);
				$id_arr='(';
				foreach($temp as $k=>$v)
				{
					$id_arr.=$v[id].",";
				}
				$id_arr.=$id.")";
				$result=$this->show_page("SELECT id,title,created,thumb,content,author FROM ".$this->table('article').' WHERE cat_id in'.$id_arr.'  ORDER BY ordering',$page,$page_size,$cfg);
				if($result)
				{
					foreach($result['result'] as $k=>$v)
					{
						$result['result'][$k]['content']=stripslashes(hy_dig_filter($v['content']));
						
						$result['result'][$k]['title']=hy_dig_filter($v['title']);
						$result['result'][$k]['author']=hy_dig_filter($v['author']);
						if(REWRITE==1)
						{
							$result['result'][$k]['url']="/a/".$v['id'].".html";
						}
						else
					    {
						   $result['result'][$k]['url']="/index.php?g=home&m=article&a=view&id=".$v['id'];
					    }
					}
					
				}
				
				$listPage['pagelink']=$result['page'];
	            $listPage['data']=$result['result'];
			
			
			 
		}
		return $listPage;
	}
	
	
	//详情页获取上一篇和下一篇文章
	public function get_pn_page($id)
	{
		$sql="SELECT a.id,a.title FROM ".$this->table('article')." as a,".$this->table('article')." as b  WHERE a.cat_id=b.cat_id AND a.id > '".$id."' AND b.id= '".$id."'  ORDER BY id ASC LIMIT 1";
		$page['next']=$this->getRow($sql);
		$page['next']['title']=hy_dig_filter($page['next']['title']);
		if(REWRITE==1)
		{
			$page['next']['url']="/a/".$page['next']['id'].".html";
		}
		else
		{
		$page['next']['url']="index.php?g=home&m=article&a=view&id=".$page['next']['id'];
		}
		
		$sql="SELECT a.id,a.title FROM ".$this->table('article')." as a,".$this->table('article')." as b  WHERE a.cat_id=b.cat_id AND a.id < '".$id."' AND b.id= '".$id."' ORDER BY id DESC LIMIT 1";
		$page['prev']=$this->getRow($sql);
		$page['prev']['title']=hy_dig_filter($page['prev']['title']);
		if(REWRITE==1)
		{
			$page['prev']['url']="/a/".$page['prev']['id'].".html";
		}
		else
		{
		   $page['prev']['url']="index.php?g=home&m=article&a=view&id=".$page['prev']['id'];
		}
		return $page;
	}
	public function art_click($id)
	{
		$sql="UPDATE ".$this->table('article')." SET hits=hits+1 WHERE id='".$id."'";
		return $res=$this->query($sql);
	}
	public function article_cat($id)
	{
		
		if($id)
		{
			$sql="SELECT a.cat_id,c.code FROM ".$this->table('article')." as a LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE a.id = '".$id."'";
			return $this->getRow($sql);
		}
	}
	public function get_single_page($id)
	{
		$result=array();
		if($id)
		{
			$sql="SELECT title,content,form_id FROM ".$this->table('article')." WHERE cat_id = '".$id."' ORDER BY id ASC LIMIT 1";
			$result=$this->getRow($sql);
			if($result)
			{
				$result['ch_title']=hy_dig_filter($result['title'],1);
				$result['en_title']=hy_dig_filter($result['title'],2);
				$result['title']=hy_dig_filter($result['title']);
				$result['author']=hy_dig_filter($result['author']);
				$result['content']=stripslashes(hy_dig_filter($result['content']));
	
				
			}
			
		}
		
		return $result;
	}
	public function get_art($id)
	{
		$result=array();
		if($id)
		{
		   $sql="SELECT title,abstract,keywords,author,created,source_url,content,page_title,hits,source,thumb FROM ".$this->table('article')." WHERE id = '".$id."'";
			
		   $result=$this->getRow($sql);
		   if($result)
		   {
		   	$result['title']=hy_dig_filter($result['title']);
		   	$result['author']=hy_dig_filter($result['author']);
		   	$result['content']=stripslashes(hy_dig_filter($result['content']));
		  
		 
		   	
			return $result;
		   }
		   else
		  {
			return $result;
		   }
		}
		
		return $result;
	}
	
}